package com.ch.doudemo.util;

import android.util.Log;

import com.zhy.http.okhttp.callback.Callback;

import okhttp3.Response;

/**
 * Created by ColorRain on 2018/8/6.
 */

public abstract class MyStringCallback extends Callback<String> {
    @Override
    public boolean validateReponse(Response response, int id) {
        return true;
    }

    @Override
    public String parseNetworkResponse(Response response, int id) throws Exception {

//        if (response.code() >= 200 && response.code() < 300) {
        if (response.code() == 200) {
            return response.body().string();
        } else {
            String errorBodyStr = response.body().string();
//            if (StringUtil.isEmpty(errorBodyStr)) {
//                ErrorMessageBean.ErrorBody tempErroBody = new ErrorMessageBean.ErrorBody();
//                errorBodyStr = JSON.toJSONString(tempErroBody);
//            }
            String errorMsg = "{ \"code\" :" + response.code() + ",\"errorBody\":" + errorBodyStr + "}";
            Log.e("MyStringCallback", "errorMsg = " + errorMsg);
            if (errorMsg.contains("<html>")) {
                throw new Exception("{ \"code\" :" + response.code());
            } else {
                throw new Exception(errorMsg);
            }
        }
    }

}